import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from django.utils import simplejson

class Statistics(db.Model):
  date = db.DateTimeProperty(auto_now_add=True) 
  email = db.EmailProperty()
  name = db.StringProperty()
  vs = db.StringProperty()
  desc = db.StringProperty(multiline=True)
  dbl_flts = db.IntegerProperty()
  aces = db.IntegerProperty()
  sec_aces = db.IntegerProperty()
  winners = db.IntegerProperty()
  sec_winners = db.IntegerProperty()
  net_errs = db.IntegerProperty()
  unf_errs = db.IntegerProperty()
  points = db.IntegerProperty()
  serves = db.IntegerProperty()

  p1score = db.IntegerProperty()
  p2score = db.IntegerProperty()
  p1set1 = db.IntegerProperty()
  p1set2 = db.IntegerProperty()
  p1set3 = db.IntegerProperty()
  p2set1 = db.IntegerProperty()
  p2set2 = db.IntegerProperty()
  p2set3 = db.IntegerProperty()
  curset = db.IntegerProperty()
  matchover = db.IntegerProperty()


class Store(webapp.RequestHandler):
  def get(self):
    email = self.request.get('email')
    stat = None

    if email:
        stats = db.GqlQuery("SELECT * FROM Statistics WHERE email = :1 AND \
                matchover = :2 ORDER BY date ASC LIMIT 10", email, 0)
        stat = stats.get()

    if not stat:
        stat = Statistics()

    stat.email = email
    stat.name  = self.request.get('name')
    stat.vs = self.request.get('vs')
    stat.desc = self.request.get('desc')
 
    stat.dbl_flts = int(self.request.get('dbl_flts'))
    stat.aces = int(self.request.get('aces'))
    stat.sec_aces = int(self.request.get('sec_aces'))
    stat.winners = int(self.request.get('winners'))
    stat.sec_winners = int(self.request.get('sec_winners'))
    stat.net_errs = int(self.request.get('net_errs'))
    stat.unf_errs = int(self.request.get('unf_errs'))
    stat.points = int(self.request.get('points'))
    stat.serves = int(self.request.get('serves'))

    stat.p1score = int(self.request.get('p1score'))
    stat.p2score = int(self.request.get('p2score'))
    stat.p1set1 = int(self.request.get('p1set1'))
    stat.p2set1 = int(self.request.get('p2set1'))
    stat.p1set2 = int(self.request.get('p1set2'))
    stat.p2set2 = int(self.request.get('p2set2'))
    stat.p1set3 = int(self.request.get('p1set3'))
    stat.p2set3 = int(self.request.get('p2set3'))
    stat.curset = int(self.request.get('curset'))
    stat.matchover = int(self.request.get('mo'))

    stat.put()
    self.response.out.write("%s %s %s" % (stat.date, stat.email, stat.vs))


class Retrieve(webapp.RequestHandler):
  def get(self):
    email = self.request.get('email')
    jsondata = self.request.get('json')
    if email == "":
        if jsondata == "1":
            self.response.out.write("Please provide with the email id")
            return
        else:
            stats = db.GqlQuery("SELECT * FROM Statistics ORDER BY date ASC LIMIT 100")
    else:
        stats = db.GqlQuery("SELECT * FROM Statistics WHERE email = :1 AND matchover = :2 ORDER BY date ASC LIMIT 10", email, 0)

    stat = stats.get()

    if not stat:
        return
        
    if jsondata == "1":
        data = []
        item = {'p1name' : stat.name, 'p2name' : stat.vs,
                'dbl_flts' : stat.dbl_flts, 'net_errs' : stat.net_errs, 'unf_errs' : stat.unf_errs,
                'aces' : stat.aces, 'sec_aces' : stat.sec_aces,
                'winners' : stat.winners, 'sec_winners' : stat.sec_winners,
                'points' : stat.points, 'serves' : stat.serves,
                'p1score' : stat.p1score, 'p2score' : stat.p2score,
                'p1set1' : stat.p1set1, 'p2set1' : stat.p2set1,
                'p1set2' : stat.p1set2, 'p2set2' : stat.p2set2,
                'p1set3' : stat.p1set3, 'p2set3' : stat.p2set3,
                'curset' : stat.curset
                }

        data.append(item)
        self.response.out.write(simplejson.dumps(data))
    else:
        self.response.out.write("""<table border=1 cellspacing=1px cellpadding=1px style="border-collapse: collapse;"><tr>
                                <th>Date</th> <th>Email</th>
                                <th>Name</th> <th>Opponent</th>
                                <th>Double Faults</th> <th>Net Errors</th> <th>Unforced Errors</th>
                                <th>Aces</th> <th>2nd serv aces</th> 
                                <th>winners</th> <th>2nd serv winners</th> 
                                <th>Points</th> <th>Serves</th> 
                                <th>Score</th> <th>Score</th> 
                                <th>Set1</th> <th>Set1</th> 
                                <th>Set2</th> <th>Set2</th> 
                                <th>Set3</th> <th>Set3</th> 
                                <th>curset</th> 
                                <th>mo</th>
                                <th>Desc</th>
                                </tr>""")

        for stat in stats:
#            self.response.out.write("""<tr>
#                            <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td>
#                            <td>%d</td> <td>%d</td> <td>%d</td>
#                            </tr>""" % (
#                            stat.date, stat.email, stat.name, stat.vs,
#                            stat.dbl_flts, stat.net_errs, stat.unf_errs
#                            ))
#            self.response.out.write("</table>")
            self.response.out.write("""<tr>
                            <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td>
                            <td>%d</td> <td>%d</td> <td>%d</td>
                            <td>%d</td> <td>%d</td>
                            <td>%d</td> <td>%d</td>
                            <td>%d</td> <td>%d</td>
                            <td>%d</td> <td>%d</td>
                            <td>%d</td> <td>%d</td>
                            <td>%d</td> <td>%d</td>
                            <td>%d</td> <td>%d</td>
                            <td>%d</td>
                            <td>%d</td>
                            <td>%s</td>
                            </tr>""" % (
                            stat.date, stat.email, stat.name, stat.vs, 
                            stat.dbl_flts, stat.net_errs, stat.unf_errs,
                            stat.aces, stat.sec_aces, 
                            stat.winners, stat.sec_winners, 
                            stat.points, stat.serves, 
                            stat.p1score, stat.p2score, 
                            stat.p1set1, stat.p2set1, 
                            stat.p1set2, stat.p2set2, 
                            stat.p1set3, stat.p2set3, 
                            stat.curset,
                            stat.matchover,
                            stat.desc
                            ))

            self.response.out.write("</table>")


application = webapp.WSGIApplication([('/store', Store), ('/retrieve', Retrieve)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
